package cn.javass.dp.command.example4;

import java.util.ArrayList;
import java.util.List;

/**
 * 计算器  等于机箱
 */
public class Calculator {

    private Command addCmd = null;
    private Command substractCmd = null;

    private List<Command> undoCmds = new ArrayList<>();
    private List<Command> redoCmds = new ArrayList<>();


    public void setAddCmd(Command addCmd) {
        this.addCmd = addCmd;
    }

    public void setSubstractCmd(Command substractCmd) {
        this.substractCmd = substractCmd;
    }

    public void addPressed() {

        this.addCmd.execute();
        undoCmds.add(addCmd);
    }

    public void substractPressed() {
        this.substractCmd.execute();
        undoCmds.add(substractCmd);
    }


    public void undoPressed() {
        if (undoCmds.size() > 0) {
            Command cmd = undoCmds.get(undoCmds.size()-1);
            cmd.undo();
            this.redoCmds.add(cmd);
            this.undoCmds.remove(cmd);

        } else {
            System.out.println("没有可以撤销的操作");
        }
    }

    public void redoPressed() {
        if (redoCmds.size() > 0) {
            Command cmd = redoCmds.get(redoCmds.size()-1);
            cmd.execute(); //这里不是undo 是重新运算
            this.undoCmds.add(cmd);
            this.redoCmds.remove(cmd);

        } else {
            System.out.println("没有可以恢复的操作");
        }
    }
}
